package com.ys.service.finance.impl;

import java.math.BigDecimal;

import com.ys.entity.finance.TcwAccountCountForCompany;
import com.ys.entity.finance.TcwOperateFrozenRunning;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.sys.TsUserChange;
import com.ys.mapper.finance.TcwOperateFrozenRunningMapper;
import com.ys.service.storage.impl.TccDistributeImpl;
import com.ys.util.shiro.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.mapper.finance.FinanceMapper;
import com.ys.mapper.finance.TcwAccountCountForCompanyMapper;
import com.ys.service.finance.TcwAccountCountForCompanyService;

@Service("tcwAccountCountForCompanyService")
public class TcwAccountCountForCompanyImpl implements TcwAccountCountForCompanyService {

	private static final Logger logger = LoggerFactory.getLogger(TccDistributeImpl.class);


	@Autowired
	private FinanceMapper financeMapper;
	
	@Autowired
	private TcwAccountCountForCompanyMapper tcwAccountCountForCompanyMapper;

	@Autowired
	private TcwOperateFrozenRunningMapper tcwOperateFrozenRunningMapper;


	/**
	 * 冻结，同时添加流水
	 * @param vgsbm 运营公司编码
	 * @param amount 金额（>0）
	 * @param sourceType 来源类型（见TcwOperateFrozenRunningEnum）
	 * @param sourceId 来源id
	 * @param sourceBid 来源单号（可为null）
	 * @param remark 备注（可为null）
	 */
	@Override
	@Transactional
	public void frozen(String vgsbm, BigDecimal amount,int sourceType,long sourceId,String sourceBid,String remark) throws Exception {
		// 验证运营公司编码
		if (vgsbm == null || vgsbm.length() == 0) {
			throw new RuntimeException("公司编码有误");
		}
		
		// 验证金额
		if (amount.compareTo(BigDecimal.ZERO) <= 0) {
			throw new RuntimeException("金额必须为正");
		}
		//冻结
		financeMapper.frozenTcwAccountCountForCompany(vgsbm, amount);

		try {
			//冻结流水
			TcwAccountCountForCompany tcwAccountCountForCompany = new TcwAccountCountForCompany().setVGsBm(vgsbm).queryFirst();
			if(tcwAccountCountForCompany!=null && tcwAccountCountForCompany.getId()!=null){

				TcwOperateFrozenRunning tcwOperateFrozenRunning = new TcwOperateFrozenRunning();
				tcwOperateFrozenRunning.setSourceType(sourceType);
				tcwOperateFrozenRunning.setSourceId(sourceId);
				if(sourceBid!=null) tcwOperateFrozenRunning.setSourceBid(sourceBid);
				if(remark!=null) tcwOperateFrozenRunning.setRemark(remark);
				TsUserChange tsUserChange = new TsUserChange().setUserId(ShiroUtils.getUserId()).setStatus(1).queryFirst();
				if(tsUserChange!=null && tsUserChange.getId()!=null){
					tcwOperateFrozenRunning.setCreateChangeId(tsUserChange.getId());
				}
				tcwOperateFrozenRunning.setVgsbm(vgsbm);
				tcwOperateFrozenRunning.setChangeFdjje(amount);
				if(tcwAccountCountForCompany.getFDjJe()!=null)tcwOperateFrozenRunning.setFdjje(tcwAccountCountForCompany.getFDjJe());
				tcwOperateFrozenRunning.setStatus(1);
				tcwOperateFrozenRunningMapper.insert(tcwOperateFrozenRunning);
			}

		}catch (Exception e){
			e.printStackTrace();
			logger.error("解冻流水失败：" +e.getMessage(),e.getMessage(),e);
		}

	}

	/**
	 * 解冻，同时添加流水
	 * @param vgsbm 运营公司编码
	 * @param amount 金额（>0）
	 * @param sourceType 来源类型（见TcwOperateFrozenRunningEnum）
	 * @param sourceId 来源id
	 * @param sourceBid 来源单号（可为null）
	 * @param remark 备注（可为null）
	 */
	@Override
	@Transactional
	public void thaw(String vgsbm, BigDecimal amount,int sourceType,long sourceId,String sourceBid,String remark) throws Exception {
		// 验证运营公司编码
		if (vgsbm == null || vgsbm.length() == 0) {
			throw new RuntimeException("公司编码有误");
		}
		
		// 验证金额
		if (amount.compareTo(BigDecimal.ZERO) <= 0) {
			throw new RuntimeException("金额必须为正");
		}

		//解冻
		financeMapper.thawTcwAccountCountForCompany(vgsbm, amount);

		try {
			//解冻流水
			TcwAccountCountForCompany tcwAccountCountForCompany = new TcwAccountCountForCompany().setVGsBm(vgsbm).queryFirst();
			if(tcwAccountCountForCompany!=null && tcwAccountCountForCompany.getId()!=null){

				TcwOperateFrozenRunning tcwOperateFrozenRunning = new TcwOperateFrozenRunning();
				tcwOperateFrozenRunning.setSourceType(sourceType);
				tcwOperateFrozenRunning.setSourceId(sourceId);
				if(sourceBid!=null) tcwOperateFrozenRunning.setSourceBid(sourceBid);
				if(remark!=null) tcwOperateFrozenRunning.setRemark(remark);
				String userId = "";
				try {
					userId = ShiroUtils.getUserId();
				}catch (Exception e){
					userId = "admin";
				}
				TsUserChange tsUserChange = new TsUserChange().setUserId(userId).setStatus(1).queryFirst();
				if(tsUserChange!=null && tsUserChange.getId()!=null){
					tcwOperateFrozenRunning.setCreateChangeId(tsUserChange.getId());
				}
				tcwOperateFrozenRunning.setVgsbm(vgsbm);
				tcwOperateFrozenRunning.setChangeFdjje(amount.negate());
				if(tcwAccountCountForCompany.getFDjJe()!=null)tcwOperateFrozenRunning.setFdjje(tcwAccountCountForCompany.getFDjJe());
				tcwOperateFrozenRunning.setStatus(1);
				tcwOperateFrozenRunningMapper.insert(tcwOperateFrozenRunning);
			}
		}catch (Exception e){
			e.printStackTrace();
			logger.error("解冻流水失败：" +e.getMessage(),e.getMessage(),e);
		}

	}


	/**
	 * 修改负载额度
	 */
	@Override
	@Transactional
	public void updateFzed(String vgsbm, BigDecimal fFzEd) throws Exception {
		// 验证运营公司编码
		if (vgsbm == null || vgsbm.length() == 0) {
			throw new RuntimeException("公司编码有误");
		}

		financeMapper.updateFzed(vgsbm, fFzEd);
	}
}
